package com.my.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.my.test.pojo.Student;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class JsonDemo {

    @Test
    public void Json2Bean(){
        String string = "{\"name\":\"小明\",\"age\":10,\"sex\":\"男\"}";
        Student student = JSON.parseObject(string, new TypeReference<Student>(){});
        System.out.println(student);
    }

    @Test
    public void Bean2Json(){
        Student student = new Student("小明","10","男");
        String json = JSON.toJSONString(student);
        System.out.println(json);
    }


    @Test
    public void Json2Map(){
        String string = "{\"name\":\"小明\",\"age\":10,\"sex\":\"男\"}";
        Map<String,String> map = JSON.parseObject(string, new TypeReference<Map<String,String>>(){});
        System.out.println(map);
    }

    @Test
    public void Map2Json(){
        Map<String,String> map = new HashMap<>();
        map.put("name","小明");
        map.put("age","10");
        map.put("sex","男");
        String json = JSON.toJSONString(map);

        System.out.println(json);
    }

    @Test
    public void Json2JsonObject2JSON(){
        String string = "{\"name\":\"小明\",\"age\":10,\"sex\":\"男\"}";
        JSONObject jsonObject = JSON.parseObject(string);
        System.out.println(jsonObject);
        Student student = new Student("小李","15","女");
        String str  =  JSONObject.toJSONString(student);
        System.out.println(str);


    }

}
